﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmUserDetail
    /// Version:1.0
    /// Date:2022-11-23
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为用户设置
    /// </remarks>
    public partial class frmUserDetail : DevComponents.DotNetBar.OfficeForm
    {
        public int m_nOperateKind = 0;//0:查看 1:新增 2:修改 
        public Boolean m_bIsRefresh = false;    //是否需刷新
        public UserInfo m_systemUser = new UserInfo();

        public frmUserDetail()
        {
            InitializeComponent();
        }

        #region 01 自定义方法
        /// <summary>
        /// 数据初始化
        /// </summary>
        private void _init()
        {
            // 修改时取数据
            if (m_systemUser.m_nId != 0) {
                m_systemUser._getInfo();
            }
            txtCode.Text = m_systemUser.m_strCode;
            txtName.Text = m_systemUser.m_strName;
            txtCreateName.Text = m_systemUser.m_strCreateName;
            txtCreateTime.Text = m_systemUser.m_strCreateTime;
            txtPassword.Text = m_systemUser.m_strPassword;
            txtRemark.Text = m_systemUser.m_strRemark;
            _initDept();
            _initRole();
            cbIsNew.Visible = (m_systemUser.m_nOperate == 1);
            txtCode.Focus();
            txtCode.ReadOnly = (m_systemUser.m_nOperate != 1);
            txtName.ReadOnly = false;
        }

        /// <summary>
        /// 部门
        /// </summary>
        private void _initDept()
        {
            // 01 取客户信息
            string strSql = "";
            strSql = "SELECT Name + '|' + Code as DeptName FROM TH_System_Dept order by Code desc";
            DataTable dt = AppInfo._openSql_DataTable(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                FunctionComm._setInfoToCbb_Dt(cbbDeptName, dt);
                cbbDeptName.Text = "";
                // 初始值
                if (!m_systemUser.m_strDeptCode.Equals("")) {
                    for (int i = 0; i < cbbDeptName.Items.Count; i++) {
                        if (cbbDeptName.Items[i].ToString().Split('|')[1].Equals(m_systemUser.m_strDeptCode)) {
                            cbbDeptName.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }            
        }

        /// <summary>
        /// 部门
        /// </summary>
        private void _initRole()
        {
            // 01 取客户信息
            string strSql = "";
            strSql = "SELECT Name,Code FROM TH_System_Role order by Code desc";
            DataTable dt = AppInfo._openSql_DataTable(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                cbbRole.DataSource = dt;
                cbbRole.ValueMember = "Code";
                cbbRole.DisplayMember = "Name";
                cbbRole.IsMultiSelect = true;
                cbbRole.Text = "";
                // 初始值
                if (!m_systemUser.m_strRole.Equals(""))
                {
                    cbbRole.Text = DataDeal._getRoleName(m_systemUser.m_strRole);
                    cbbRole.ValueEx = m_systemUser.m_strRole;
                    for(int i=0;i<m_systemUser.m_strRole.Split(',').Length;i++){
                        cbbRole.SelectItems.Add(m_systemUser.m_strRole.Split(',')[i],cbbRole.Text.Split(',')[i]);
                    }
                }
            }
        }  
       
        /// <summary>
        /// 保存扫码信息
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="bIsNew"></param>
        private Boolean _saveCode()
        {
            Boolean bResult = false;
            m_systemUser.m_strCode = txtCode.Text.Trim();
            m_systemUser.m_strName = txtName.Text.Trim();
            m_systemUser.m_strCreateName = txtName.Text.Trim();
            m_systemUser.m_strDeptCode = (cbbDeptName.SelectedIndex < 0) ? "" : cbbDeptName.Text.Trim().Split('|')[1];
            m_systemUser.m_strDeptName = (cbbDeptName.SelectedIndex < 0) ? "" : cbbDeptName.Text.Trim().Split('|')[0];
            m_systemUser.m_strPassword = txtPassword.Text.Trim();
            // 小于32位进行加密
            if (m_systemUser.m_strPassword.Length < 32) {
                m_systemUser.m_strPassword = FunctionComm._encryptMD5(m_systemUser.m_strPassword);
            }
            m_systemUser.m_strRole = (cbbRole.Items.Count < 1 || cbbRole.SelectedValues.Length < 1) ? "" : cbbRole.ValueEx;
            m_systemUser.m_strRemark = txtRemark.Text.Trim();

            if (m_systemUser.m_strCode.Length < 1)
            {
                CommFun._messageBoxShow("编号不能为空!");
                return false;
            }

            if (m_systemUser.m_strName.Length < 1)
            {
                CommFun._messageBoxShow("名称不能为空!");
                return false;
            }

            if (m_systemUser.m_strDeptCode.Length < 1)
            {
                CommFun._messageBoxShow("部门不能为空!");
                return false;
            }

            if (m_systemUser.m_strPassword.Length < 1)
            {
                CommFun._messageBoxShow("口令不能为空!");
                return false;
            }
            
            // 保存数据
            if (!m_systemUser._checkName())
            {
                return false;
            }

            if (m_systemUser._saveInfo() < 1)
            {
                CommFun._messageBoxShow("数据保存失败");
            }
            else
            {
                m_bIsRefresh = true;
                CommFun._messageBoxShow("数据保存成功");
                if (cbIsNew.Checked)    // 继续新增
                {
                    m_systemUser._init();
                    _init();
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }                
            }
            return bResult;
        }
        
        #endregion

        #region  02 窗体相关事件方法
        private void frmConfig_Load(object sender, EventArgs e)
        {
            // 初始化
            _init();
            cbIsNew.Checked = m_systemUser.m_nOperate == 1;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            _saveCode();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            CommFun._cbbDrawItem(sender, e);
        }

        private void cbbRole_KeyDown(object sender, KeyEventArgs e)
        {
            //e.Handled = true;
            return;
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // 禁止部门,角色及数据权限输入
            if (cbbRole.Focused || cbbDeptName.Focused)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
